package com.asdf.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

/**
 * @author c0rwin
 *
 */
public class ConnectDialog extends Dialog
{

	private static final long	serialVersionUID	= - 6555640506479754289L;

	private static final String	LOGIN_LABEL_CAPTION		= "Login:";
	private static final String	PASSWORD_LABEL_CAPTION	= "Password:";

	public static final String	CONNECT		= "Connect";
	public static final String  NEW_USER 	= "New User";
	public static final String	CANCEL		= "Cancel";

	private JPanel inputPanel 	= new JPanel( new GridLayout( 0, 2));
	private JPanel buttonPanel 	= new JPanel( new FlowLayout( FlowLayout.CENTER));
	
	private JLabel lblLogin 	= new JLabel( LOGIN_LABEL_CAPTION);
	private JTextField txtLogin	= new JTextField( ); 

	private JLabel lblPassword	  		= new JLabel( PASSWORD_LABEL_CAPTION);
	private JPasswordField pwdPassword 	= new JPasswordField( );

	private JButton btnConnect = new JButton( CONNECT);
	private JButton btnNewUser = new JButton( NEW_USER);
	private JButton btnCancel  = new JButton( CANCEL);

	
	/* (non-Javadoc)
	 * @see com.asdf.ui.Dialog#drawLabelsAndInputs()
	 */
	protected void drawLabelsAndInputs( )
	{
		
		inputPanel.add( lblLogin);
		inputPanel.add( txtLogin);
		
		inputPanel.add( lblPassword);
		inputPanel.add( pwdPassword);
		
		buttonPanel.add( btnNewUser);
		buttonPanel.add( btnConnect);
		buttonPanel.add( btnCancel);
		
		TitledBorder title = BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.black),
										  "Enter your login details:", TitledBorder.LEFT, TitledBorder.TOP);
		inputPanel.setBorder( title);
		add( inputPanel);
		add( buttonPanel);
	}
	
	/**
	 * 
	 */
	public ConnectDialog( )
	{
		super( "Connection dialog");
		drawLabelsAndInputs( );
		pack( );
	}
	
	/**
	 * @param commandName
	 * @param listener
	 */
	public void registerEventHandlerConnectCommand( String commandName, ActionListener listener)
	{
		btnConnect.setActionCommand( commandName);
		btnConnect.addActionListener( listener);
	}
	
	/**
	 * @param commandName
	 * @param listener
	 */
	public void registerEventHandlerCancelCommand( String commandName, ActionListener listener)
	{
		btnCancel.setActionCommand( commandName);
		btnCancel.addActionListener( listener);
	}
	
	/**
	 * @param commandName
	 * @param listener
	 */
	public void registerEventHandlerNewUserCommand( String commandName, ActionListener listener)
	{
		btnNewUser.setActionCommand( commandName);
		btnNewUser.addActionListener( listener);
	}
	
	/**
	 * @return
	 */
	public String getUserLogin( )
	{
		return txtLogin.getText( );
	}
	
	/**
	 * @return
	 */
	public String getUserPwd( )
	{
		return new String ( pwdPassword.getPassword( ));
	}
	/**
	 * @param args
	 */
	public static void main( String [ ] args)
	{
		final ConnectDialog connect = new ConnectDialog( );
		connect.setVisible( true);
		connect.registerEventHandlerCancelCommand( "CancelConnect", new ActionListener( )
																	{
																		@Override
																		public void actionPerformed(
																				ActionEvent e)
																		{
																			if ( e.getActionCommand( ).equalsIgnoreCase( "CancelConnect"))
																				connect.dispose( );
																			
																		}
																	});
		connect.registerEventHandlerConnectCommand( "", new ActionListener( )
		{
			@Override
			public void actionPerformed(
					ActionEvent e)
			{
				System.out.println( connect.getUserPwd( ));
			}
		});
	}

}
